package com.hcc.repository.extension.listener;

import com.hcc.repository.annotation.PropSetListener;

/**
 * 数据权限监听器
 *
 * @author hushengjun
 * @date 2023/8/23
 */
public interface DataPermissionPropSetListener extends PropSetListener {

    @Override
    default Object onPropSet(Object entity, Object value, String propName, String columnName) {
        if (!hasPermission(entity, value, propName, columnName)) {
            return valueForNoPermission(entity, value, propName, columnName);
        }

        return value;
    }

    /**
     * 是否有该字段权限
     * @param entity
     * @param value
     * @param propName
     * @param columnName
     * @return
     */
    default boolean hasPermission(Object entity, Object value, String propName, String columnName) {
        return true;
    }

    /**
     * 没有权限时的返回值
     * @param entity
     * @param value
     * @param propName
     * @param columnName
     * @return
     */
    default Object valueForNoPermission(Object entity, Object value, String propName, String columnName) {
        return null;
    }

}
